package global

import (
	"longmen/server/pkg/common/helper/const"
	"regexp"

	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	intrans "github.com/go-playground/validator/v10/translations/zh"
)

func InitValidate() error {
	_const.Trans, _ = ut.New(zh.New()).GetTranslator("zh")
	err := intrans.RegisterDefaultTranslations(_const.Validate, _const.Trans)
	if err != nil {
		return err
	}
	err = _const.Validate.RegisterValidation("cn_mobile", func(fl validator.FieldLevel) bool {
		mobile := fl.Field().String()
		re := `^1[3456789]\d{9}$`
		r := regexp.MustCompile(re)
		return r.MatchString(mobile)
	})
	if err != nil {
		Log.Fatal("validate extend error: ", err)
	}
	return nil
}
